﻿using HZH_Controls.Controls;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 渐变.user_Form
{
    public partial class user_returnFrm : Form
    {
        public user_returnFrm()
        {
            InitializeComponent();
        }
        public string opname { get; set; }
        public void loadMsg()
        {
            List<DataGridViewColumnEntity> lstCulumns = new List<DataGridViewColumnEntity>();
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "RentID", HeadText = "订单编号", Width = 20, WidthType = SizeType.AutoSize });
            //lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "price", HeadText = "应付金额", Width = 20, WidthType = SizeType.AutoSize });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "carname", HeadText = "车名", Width = 50, WidthType = SizeType.AutoSize });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "opername", HeadText = "租车人", Width = 20, WidthType = SizeType.AutoSize });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Paytime", HeadText = "付款时间", Width = 20, WidthType = SizeType.Percent });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "begindate", HeadText = "起始时间", Width = 40, WidthType = SizeType.Percent });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "returntime", HeadText = "归还时间", Width = 40, WidthType = SizeType.Percent });
            this.ucDataGridView1.Columns = lstCulumns;
            this.ucDataGridView1.IsShowCheckBox = true;
            List<object> lstSource = new List<object>();
            string sql = "select * from carRents where opername=@opername and isPay='True' and rentflag =0";
            SqlParameter p = new SqlParameter("@opername", this.opname);
            DataTable dt = new DataTable();
            dt = DBHelper.ExecuteQuery(sql, p);
            this.ucDataGridView1.DataSource = dt;
            this.ucDataGridView1.First();
        }
        private void user_returnFrm_Load(object sender, EventArgs e)
        {
            this.Dock = DockStyle.Fill;
            loadMsg();
        }

        private void btnReturn_Click(object sender, EventArgs e)
        {
            //rentflag变为1, 车的状态为true
            DialogResult dir = MessageBox.Show("您确定要归还车辆吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dir == DialogResult.OK)
            {
                string id = (ucDataGridView1.SelectRow.DataSource as DataRow)["RentID"].ToString();

                string sql = "update  carRents set rentflag = 1 where RentID=@RentID";
                SqlParameter[] ps =
                {
                    new SqlParameter("@RentID",id),

                };
                int result = DBHelper.ExecuteNonQuery(sql, ps);
                if (result > 0)
                {
                    this.ShowSuccessTip("还车成功！");
                    string carname = (ucDataGridView1.SelectRow.DataSource as DataRow)["carname"].ToString();
                    string sql2 = "update CarInfo set CStatus = 'True' where CName = @cname";
                    SqlParameter s = new SqlParameter("@cname", carname);
                    DBHelper.ExecuteNonQuery(sql2, s);
                    loadMsg();

                }
                else
                {
                    this.ShowErrorTip("还车成功！");
                }
            }
            else
            {
                return;
            }
        }
    }
}
